package com.brkj.test;

import android.graphics.drawable.Drawable;
import android.text.Html;
import com.brkj.main.R;
import com.brkj.test.model.DS_Exam_detail_ques;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import com.brkj.util.ConstAnts;
import com.brkj.util.ImgShow;
import com.dgl.sdk.util.TextViewWithImage;

import java.net.URL;

public class Question {
	public final static int SINGLE_SELECT = 1;
	public final static int MULTI_SELECT = 2;
	public final static int FILL_BLANK = 3;
	public final static int JUDGEMENT = 4;
	private DS_Exam_detail_ques question;
	private Context context;
	private View view;
	private CheckBox multiSelectA,multiSelectB,multiSelectC,multiSelectD,multiSelectE;
	private RadioButton selectA,selectB,selectC,selectD,selectE;
	private EditText asnEditText;
	private RadioButton selectTrue, selectFalse;
	
	public Question(Context context, DS_Exam_detail_ques question){
		this.context = context;
		this.question = question;
	}

    public int getUTID(){
        return question.getUTID();
    }

    public int getType(){
        return question.getType();
    }

	protected View getView() {
		switch (question.getType()) {
		case FILL_BLANK:
			view = LayoutInflater.from(context).inflate(R.layout.test_question_fillblank, null);
			TextView questionView = (TextView) view.findViewById(R.id.question);
			asnEditText = (EditText) view.findViewById(R.id.asnEditText);
			questionView.setText(question.getQuestion());
			break;
		case SINGLE_SELECT:
			view = LayoutInflater.from(context).inflate(R.layout.test_question_single_select, null);
			TextView questionView2 = (TextView) view.findViewById(R.id.question);
			selectA = (RadioButton) view.findViewById(R.id.selectA);
			selectB = (RadioButton) view.findViewById(R.id.selectB);
			selectC = (RadioButton) view.findViewById(R.id.selectC);
			selectD = (RadioButton) view.findViewById(R.id.selectD);
			selectE = (RadioButton) view.findViewById(R.id.selectE);

//			questionView2.setText("单选题:\n\n"+question.getQuestion());
//            questionView2.setText(Html.fromHtml(sText, imageGetter, null));

//			selectA.setText("A."+question.getSelectA());
//			selectB.setText("B."+question.getSelectB());
//			selectC.setText("C."+question.getSelectC());
//			selectD.setText("D."+question.getSelectD());
//			if((question.getSelectE() == "") ||(question.getSelectE() == null)){
//				selectE.setVisibility(View.GONE);
//			}else{
//				selectE.setVisibility(View.VISIBLE);
//				selectE.setText(question.getSelectE());
//			}

            textViewShow(questionView2, "单选题:\n\n"+question.getQuestion());
            textViewShow(selectA, "A."+question.getSelectA());
            textViewShow(selectB, "B."+question.getSelectB());
            textViewShow(selectC, "C."+question.getSelectC());
            textViewShow(selectD, "D."+question.getSelectD());
			if((question.getSelectE() == "") ||(question.getSelectE() == null)){
				selectE.setVisibility(View.GONE);
			}else{
				selectE.setVisibility(View.VISIBLE);
                textViewShow(selectE, "E."+question.getSelectE());
			}

			break;
		case MULTI_SELECT:
			view = LayoutInflater.from(context).inflate(R.layout.test_question_multi_select, null);
			TextView questionView3 = (TextView) view.findViewById(R.id.question);
			multiSelectA = (CheckBox) view.findViewById(R.id.multiSelectA);
			multiSelectB = (CheckBox) view.findViewById(R.id.multiSelectB);
			multiSelectC = (CheckBox) view.findViewById(R.id.multiSelectC);
			multiSelectD = (CheckBox) view.findViewById(R.id.multiSelectD);
			multiSelectE = (CheckBox) view.findViewById(R.id.multiSelectE);
			
//			questionView3.setText("多选题\n\n"+question.getQuestion());
//			multiSelectA.setText("A."+question.getSelectA());
//			multiSelectB.setText("B."+question.getSelectB());
//			multiSelectC.setText("C."+question.getSelectC());
//			multiSelectD.setText("D."+question.getSelectD());
//			if(question.getSelectE() != null){
//				if(question.getSelectE().equals("")){
//					multiSelectE.setVisibility(View.GONE);
//				}else{
//					multiSelectE.setVisibility(View.VISIBLE);
//					multiSelectE.setText("E."+question.getSelectE());
//				}
//			}else{
//				multiSelectE.setVisibility(View.GONE);
//			}

            textViewShow(questionView3, "多选题:\n\n"+question.getQuestion());
            textViewShow(multiSelectA, "A."+question.getSelectA());
            textViewShow(multiSelectB, "B."+question.getSelectB());
            textViewShow(multiSelectC, "C."+question.getSelectC());
            textViewShow(multiSelectD, "D."+question.getSelectD());
            if((question.getSelectE() == "") ||(question.getSelectE() == null)){
                multiSelectE.setVisibility(View.GONE);
            }else{
                multiSelectE.setVisibility(View.VISIBLE);
                textViewShow(multiSelectE, "E."+question.getSelectE());
            }

			break;
		case JUDGEMENT:
			view = LayoutInflater.from(context).inflate(R.layout.test_question_judge, null);
			TextView questionView4 = (TextView) view.findViewById(R.id.question);
			selectTrue = (RadioButton) view.findViewById(R.id.selectTrue);
			selectFalse = (RadioButton) view.findViewById(R.id.selectFalse);
			
//			questionView4.setText("判断题\n\n"+question.getQuestion());
            textViewShow(questionView4, "判断题:\n\n"+question.getQuestion());
			break;

		default:
			view = new TextView(context);
			((TextView)view).setText("暂无此题型！");
			break;
		}
		
		return view;
	}

   void  textViewShow(TextView view, String text){
       TextViewWithImage twi =new TextViewWithImage(context, ConstAnts.IMG_CATCH_DIR);

       twi.show(view, text);
    }

	public String getAnswer(){
		String answer = "";
		switch (question.getType()) {
		case FILL_BLANK:
			answer = asnEditText.getText().toString();
			break;
		case SINGLE_SELECT:
			if(selectA.isChecked()){
				answer = "A";
			}else if(selectB.isChecked()){
				answer = "B";
			}else if(selectC.isChecked()){
				answer = "C";
			}else if(selectD.isChecked()){
				answer = "D";
			}else if(selectE.isChecked()){
				answer = "E";
			}else{
				answer = "";
			}
			break;
		case MULTI_SELECT:

			StringBuffer str = new StringBuffer();
			if(multiSelectA.isChecked()){
				str.append("a");
			}
			
			if(multiSelectB.isChecked()){
				if(str.toString().equals("")){
					str.append("b");
				}else{
					str.append("b");
				}
			}
			
			if(multiSelectC.isChecked()){
				if(str.toString().equals("")){
					str.append("c");
				}else{
					str.append("c");
				}
			}
			
			if(multiSelectD.isChecked()){
				if(str.toString().equals("")){
					str.append("d");
				}else{
					str.append("d");
				}
			}
			if(multiSelectE.isChecked()){
				if(str.toString().equals("")){
					str.append("e");
				}else{
					str.append("e");
				}
			}
			
			answer = str.toString();
			break;
		case JUDGEMENT:
			if(selectTrue.isChecked()){
				answer = "true";
			}else if(selectFalse.isChecked()){
				answer = "false";
			}
			break;
		default:
			break;
		}
		
		return answer;
	}
	
	public void clearAnswer() {
		switch (question.getType()) {
		case FILL_BLANK:
			asnEditText.setText("");
			break;
		case SINGLE_SELECT:
			selectA.setChecked(false);
			selectB.setChecked(false);
			selectC.setChecked(false);
			selectD.setChecked(false);
			selectE.setChecked(false);
			break;
		case MULTI_SELECT:
			multiSelectA.setChecked(false);
			multiSelectB.setChecked(false);
			multiSelectC.setChecked(false);
			multiSelectD.setChecked(false);
			multiSelectE.setChecked(false);
			break;
		default:
			break;
		}
		
	}
}
